package E2013;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class e11 {
    public static void main(String[] args) {

        RandomAccessFile randomAccessFile = null;
        File file = null;
        try{
            // 声明文件
            file = new File("src/data/random.txt");
            randomAccessFile = new RandomAccessFile(file,"rw"); // 指定文件源，读写模式
            // 写入文件
            for (int i=0;i<20;i++){
                randomAccessFile.writeInt(i);
            }
            // 将指针设置在目标位置进行读取或写入
            randomAccessFile.seek(12);  // 0 1 2
            // 读取,每次读取一个整型的长度，，4个字节
            for (int i=0;i<randomAccessFile.length();i+=4){
                int a =randomAccessFile.readInt();
                System.out.println(a);
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (randomAccessFile!=null){
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
